<%@ page import="org.json.simple.*" %> <%--json_simple_1-1.jar--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    import = "java.sql.*" %>

	<%
	request.setCharacterEncoding("UTF-8");
	
	String id_a = request.getParameter("id_a");
	String url = "jdbc:oracle:thin:@155.230.193.42:1521:oraknu";
	String user = "system";
	String pass = "DBLab012345";
	Connection con = null;
	String sql = null;
	int result;
	

	final int maxMemoCount = 20;
	int pageNumber = 1;
	int category = 0;
	String title = "";

	String obj = request.getParameter("page");
	if( !(obj == null  || obj.trim().length() == 0))
		pageNumber = Integer.parseInt(obj);
	
	obj = request.getParameter("category");
	if( !(obj == null  || obj.trim().length() == 0))
		category = Integer.parseInt(obj);

	obj = request.getParameter("title");
	if( !(obj == null  || obj.trim().length() == 0))
		title = (String)obj;



	JSONObject resultJson = new JSONObject();
	
	try
	{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		System.out.println("success");
	}
	catch(ClassNotFoundException e)
	{
		System.err.println("error = " + e.getMessage());
		System.exit(1);
	}
	try
	{
		con = DriverManager.getConnection(url, user, pass);
	}
	catch(SQLException e)
	{
		System.err.println("sql error = " + e.getMessage());
		System.exit(1);
	}
	try
	{
		con.setAutoCommit(false);
		
		PreparedStatement pstmt = null;

		if(!title.equals(""))
		{

			if(category == 0)
			{
				sql = "select count(*) from article where title like '%'||?||'%'";
				pstmt = con.prepareStatement(sql);
				pstmt.setString(1, title);
			}
			else
			{	
				sql = "select count(*) from article where  ID_C = ? and title like '%'||?||'%'";
				pstmt = con.prepareStatement(sql);
				pstmt.setInt(1, category);
				pstmt.setString(2, title);
			}
		}
		else if(category == 0)
		{
			sql = "select count(*) from article";
			pstmt = con.prepareStatement(sql);
		}
		else
		{
			sql = "select count(*) from article where  ID_C = ?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, category);
		}
		ResultSet rs = pstmt.executeQuery();
		if(rs.next())
		{
			int count = rs.getInt(1);
			int pageCount = count / maxMemoCount;
			if(count % maxMemoCount != 0)
				pageCount++;
			resultJson.put("page",pageCount);
			resultJson.put("article", count);
			resultJson.put("ppa", maxMemoCount);
		}
		
		out.print(resultJson);
		pstmt.close();
		con.close();
	}
	catch(Exception e)
	{
		System.err.println("sql error = " + e.getMessage());
	}
			
%>